#include "stdafx.h"
#include "socket.h"
#include "session.h"
bool IoServicePool::shouldJoin = true;
SocketServer::SocketServer(int port,int threadsize):iosev_pool_(threadsize),
	acceptor_(iosev_pool_.getService()/*,Tcp::endpoint(ip::tcp::v4(), 1000),Acceptor::reuse_address(true)*/){

// 		Resolver resolver(acceptor_.io_service());
// 		Resolver::query query("localhost", "2323");
// 		Endpoint endpoint = *resolver.resolve(query);
		Endpoint endpoint(ip::tcp::v4(), 2323);
		acceptor_.open(endpoint.protocol());
		//acceptor_.set_option(Acceptor::reuse_address(true));
		acceptor_.bind(endpoint);
		acceptor_.listen();
}
void SocketServer::start(){
	Socket *socket = new Socket(iosev_pool_.getService());	
	acceptor_.async_accept(*socket,boost::bind(&SocketServer::on_client_accept,this,socket,_1));
}
void SocketServer::on_client_accept(Socket *socket,const ErrorCode& ec){
	if(!ec){
		//do something with socket
		SessionProtocol * protocol = new SessionProtocol(*socket);
		protocol->start();
		start();
	}else{
		delete socket;
	}
}
